package com.rinlink.jbcodec.creator;

import com.alibaba.fastjson.JSONObject;
import com.rinlink.jbcodec.BytesToObjectCreatorMap;
import com.rinlink.jbcodec.JSONCodec;
import com.rinlink.jbcodec.transform.BytesToJSON;
import com.rinlink.jbcodec.transform.Transform;

import java.nio.ByteBuffer;

public class ByteBufferToJSONCreator extends ByteBufferToObjectCreator<JSONObject> {

    private BytesToObjectCreatorMap creatorMap;

    public ByteBufferToJSONCreator(BytesToObjectCreatorMap creatorMap){
        this.creatorMap = creatorMap;
    }

    @Override
    protected Transform create(JSONObject attr, ByteBuffer data, JSONObject t) {
        JSONObject jsonScheme = attr.getJSONObject("scheme");
        JSONCodec.Scheme scheme = jsonScheme.toJavaObject(JSONCodec.Scheme.class);
        return new BytesToJSON(data,scheme,creatorMap);
    }

}
